
$(function () {

    Tabs(); //Table绑定
    setInterval(function () {  //每s刷新Table
        getDate();//赋值当前时间   
    }, 1000);


    setInterval(function () {  //每s刷新Table
        var allTableData = $('#DataGrid').bootstrapTable('getData');
       
        //$('#DataGrid').bootstrapTable('refreshOptions', { pageNumber: 1 });
        $("#DataGrid").bootstrapTable("refresh", {
            silent: true //静态刷新
        });
        $("#Agentnum").html($("#DataGrid").bootstrapTable("getData").length); //赋值当前座席数

        var rows = $('#DataGrid').bootstrapTable('getData');//行的数据
        var jsonup = "";
        for (var i = 0; i < rows.length; i++) {

            jsonup += rows[i].Curstatus;
        }

        $("#Waitingnum").html((jsonup.split('AgentState_Waiting')).length - 1); //等待
        $("#Parknum").html((jsonup.split('AgentState_Park')).length - 1); //小休
        $("#Talknum").html(((jsonup.split('AgentState_Talking')).length - 1) -
            ((jsonup.split('AgentState_TalkingWithHold')).length - 1)); //通话
        $("#Dialnum").html((jsonup.split('AgentState_Dialing')).length - 1); //拨号
        $("#Ringingnum").html((jsonup.split('AgentState_Ringing')).length - 1); //振铃
        $("#Holdnum").html((jsonup.split('AgentState_Holding')).length - 1); //保持
        $("#ACWnum").html((jsonup.split('AgentState_ACW')).length - 1); //话后处理
        $("#Confnum").html((jsonup.split('AgentState_Conf')).length - 1); //会议

    }, 5000);
});




//获取当前时间
function getDate() {
    var myDate = new Date().Format("yyyy/MM/dd hh:mm:ss"); //获取系统当前时间
    $("#iTime").html(myDate);//赋值当前时间

}

//tab绑定
function Tabs() {
    var c_url = "iifoxHandler/AgentStatus";
    var $table = $('#DataGrid');

    $table.lgbTable({
        url: c_url,
        dataBinder: {
            map: {

            },
            callback: function (data) {
            }
        },
        smartTable: {
            idField: "created",
            sortName: "created",
            sortOrder: "desc",

            queryParams: function (params) {
                return $.extend(params, {
                });
            },           //传递参数（*）
            columns: [
                {
                    title: "座席", field: "Agent", sortable: true, class: "text-center"
                },
                {
                    title: "分机", field: "ExtnNo", sortable: true, class: "text-center"
                },
                {
                    title: "当前状态", field: "Curstatus", sortable: true, class: "text-center", formatter: function (value, row, index) {
                        return ReplceStatus(value);
                    }
                },
                { title: "主叫号", field: "Caller", sortable: true, class: "text-center" },
                { title: "被叫号", field: "Callee", sortable: true, class: "text-center" },
                { title: "小休理由", field: "ParkReason", sortable: true },
                { title: "开始时间", field: "BeginTime", sortable: true, class: "text-center" },
                {
                    title: "持续时间", field: "time", sortable: true, class: "text-center", formatter: function (value, row, index) {
                        return newDate(value, row);
                    }
                },
                { title: "登录时间", field: "LoginTime", class: "text-center", sortable: true },
            ],
            editButtons: {
                events: {
                    'click': function (e, value, row, index) {
                        $table.bootstrapTable('uncheckAll');
                        $table.bootstrapTable('check', index);
                    }
                },
                formatter: function (value, row, index) {
                    var $this = this.clone();
                    if (row.IsReset === 0) {
                        $this.find('button.reset').remove();
                    }
                    return $this.html();
                }
            },
            exportOptions: {
                fileName: "座席监控",
                ignoreColumn: [0, 7]
            }
        }
    });
}


function newDate(value, row) { //将秒数转为时分秒 格式 HH:mm:ss

    var data;  
    var sta_date = new Date(row.BeginTime.replace(/-/g, "/"));
    var end_date = new Date(getDateNew());
    value = (end_date - sta_date) / 1000;
    //alert(date3);

    if (value > -1) {
        var hour = Math.floor(value / 3600);//小时
        var min = Math.floor(value / 60) % 60;//分钟
        var sec = value % 60;
        if (hour < 10) {
            data = '0' + hour + ":";
        } else {
            data = hour + ":";
        }
        if (min < 10) { data += "0"; }
        data += min + ":";
        if (sec < 10) { data += "0"; }
        data += sec.toFixed(0);
    }

    return data;

}
//获取当前时间
function getDateNew() {
    var myDate = new Date().Format("yyyy/MM/dd hh:mm:ss"); //获取系统当前时间
    return myDate//赋值当前时间

}



// 对Date的扩展，将 Date 转化为指定格式的String 
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符， 
// 年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字) 
// 例子： 
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423 
// (new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18 
Date.prototype.Format = function (fmt) { //author: meizz 
    var o = {
        "M+": this.getMonth() + 1,                 //月份 
        "d+": this.getDate(),                    //日 
        "h+": this.getHours(),                   //小时 
        "m+": this.getMinutes(),                 //分 
        "s+": this.getSeconds(),                 //秒 
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
        "S": this.getMilliseconds()             //毫秒 
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

//将英文状态转为中文
function ReplceStatus(value) {
    var _json = value;
    if (value == "AgentState_TalkingWithHold") {
        _json = _json.replace("AgentState_TalkingWithHold", "保持下通话");
    }
    else if (value == "AgentState_ACW") {
        _json = _json.replace("AgentState_ACW", "话后处理");
    }
    else if (value == "AgentState_Waiting") {
        _json = _json.replace("AgentState_Waiting", "等待");
    }
    else if (value == "AgentState_Ringing") {
        _json = _json.replace("AgentState_Ringing", "振铃");
    }
    else if (value == "AgentState_RingingBack") {
        _json = _json.replace("AgentState_RingingBack", "回铃");
    }
    else if (value == "AgentState_Talking") {
        _json = _json.replace("AgentState_Talking", "通话");
    }
    else if (value == "AgentState_BCW") {
        _json = _json.replace("AgentState_BCW", "话前处理");
    }
    else if (value == "AgentState_Park") {
        _json = _json.replace("AgentState_Park", "小休");
    }
    else if (value == "AgentState_PreDialing") {
        _json = _json.Replace("AgentState_PreDialing", "预拨号");
    }
    else if (value == "AgentState_Dialing") {
        _json = _json.replace("AgentState_Dialing", "拨号");
    }
    else if (value == "AgentState_Holding") {
        _json = _json.replace("AgentState_Holding", "保持");
    }
    else if (value == "AgentState_HELD") {
        _json = _json.replace("AgentState_HELD", "被保持");
    }
    else if (value == "AgentState_Dial_UnderHold") {
        _json = _json.replace("AgentState_Dial_UnderHold", "保持后拨号");
    }
    else if (value == "AgentState_Conf") {
        _json = _json.replace("AgentState_Conf", "会议");
    }
    else if (value == "AgentState_UnLoggoned") {
        _json = _json.replace("AgentState_UnLoggoned", "注销");
    }
    else if (value == "AgentState_Loggoned") {
        _json = _json.replace("AgentState_Loggoned", "已登录");
    }
    else if (value == "AgentState_Hangup") {
        _json = _json.replace("AgentState_Hangup", "挂机");
    }
    else if (value == "AgentState_Logging") {
        _json = _json.replace("AgentState_Logging", "登录中");
    }
    else if (value == "AgentState_TalkingFailed") {
        _json = _json.replace("AgentState_TalkingFailed", "通话失败");
    }
    else if (value == "AgentState_UnKnow") {
        _json = _json.replace("AgentState_UnKnow", "未知状态");
    }
    else if (value == "AgentState_Work") {
        _json = _json.replace("AgentState_Work", "工作");
    }
    else if (value == "AgentState_EndAcw") {
        _json = _json.replace("AgentState_EndAcw", "结束话后处理");
    }
    return _json;  //返回Replace掉的数据信息
}
